package gsw.tool.rxjava;

import gsw.tool.rxjava.operator.Function;
import gsw.tool.rxjava.operator.Operator;
import gsw.tool.rxjava.operator.OperatorMap;

/**
 * 支持数据转换的Action
 *
 * @param <T> 原Action中的数据
 * @param <R> 转换后的数据
 */
public class OnSubscribleLift<T, R> implements ObservableOnSubscribe<R> {

    //原Action
    ObservableOnSubscribe<T> parent;
    //Map操作符
    Operator<? extends R, ? super T> operator;

    public OnSubscribleLift(ObservableOnSubscribe<T> onSubscribe, Function<? super T, ? extends R> function) {
        this.parent = onSubscribe;
        this.operator = new OperatorMap<T, R>(function);
    }

    @Override
    public void subscribe(Observer<? super R> observer) {
        //使用Map操作符包装的新观察者
        Observer<? super T> st = operator.apply(observer);
        parent.subscribe(st);
    }
}
